from datetime import datetime
from typing import List
import sys
import os

# 兼容作为脚本运行的模块查找路径
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

from database.connection import SessionLocal
from database.models import User
from utils.auth import get_password_hash

def reset_passwords(usernames: List[str], new_password: str) -> None:
    """
    批量重置指定用户名的密码为给定新密码

    Args:
        usernames: 需要重置密码的用户名列表
        new_password: 新的明文密码，将按项目默认方案加密存储

    Returns:
        None
    """
    session = SessionLocal()
    try:
        hashed = get_password_hash(new_password)
        updated = []
        for uname in usernames:
            user = session.query(User).filter(User.username == uname, User.is_delete == False).first()
            if user:
                user.password = hashed
                user.update_time = datetime.now()
                updated.append(uname)
        session.commit()
        print("updated:", updated)
    finally:
        session.close()


if __name__ == "__main__":
    # 默认重置 admin 和 test 为 123123
    reset_passwords(["admin", "test"], "123123")